home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / libxpm / libxpm34.gz / libxpm34 / xpm-3.4 / lib / XpmWrFFrI.c < prev    next >
C/C++ Source or Header  |  1994-03-14  |  6KB  |  243 lines

  1. /* Copyright 1989-94 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * XpmWrFFrI.c:                                                                *
  4. *                                                                             *
  5. *  XPM library                                                                *
  6. *  Write an image and possibly its mask to an XPM file                        *
  7. *                                                                             *
  8. *  Developed by Arnaud Le Hors                                                *
  9. \*****************************************************************************/
  10.  
  11. #include "xpmP.h"
  12.  
  13. LFUNC(WriteColors, void, (FILE *file, XpmColor *colors, unsigned int ncolors));
  14.  
  15. LFUNC(WritePixels, int, (FILE *file, unsigned int width, unsigned int height,
  16.              unsigned int cpp, unsigned int *pixels,
  17.              XpmColor *colors));
  18.  
  19. LFUNC(WriteExtensions, void, (FILE *file, XpmExtension *ext,
  20.                   unsigned int num));
  21.  
  22. int
  23. XpmWriteFileFromImage(display, filename, image, shapeimage, attributes)
  24.     Display *display;
  25.     char *filename;
  26.     XImage *image;
  27.     XImage *shapeimage;
  28.     XpmAttributes *attributes;
  29. {
  30.     XpmImage xpmimage;
  31.     XpmInfo info;
  32.     int ErrorStatus;
  33.  
  34.     /* create an XpmImage from the image */
  35.     ErrorStatus = XpmCreateXpmImageFromImage(display, image, shapeimage,
  36.                          &xpmimage, attributes);
  37.     if (ErrorStatus != XpmSuccess)
  38.         return (ErrorStatus);
  39.  
  40.     /* write the file from the XpmImage */
  41.     if (attributes) {
  42.     xpmSetInfo(&info, attributes);
  43.     ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, &info);
  44.     } else
  45.     ErrorStatus = XpmWriteFileFromXpmImage(filename, &xpmimage, NULL);
  46.  
  47.     /* free the XpmImage */
  48.     XpmFreeXpmImage(&xpmimage);
  49.  
  50.     return (ErrorStatus);
  51. }
  52.  
  53. int
  54. XpmWriteFileFromXpmImage(filename, image, info)
  55.     char *filename;
  56.     XpmImage *image;
  57.     XpmInfo *info;
  58. {
  59.     xpmData mdata;
  60.     char *name, *dot, *s, new_name[BUFSIZ];
  61.     int ErrorStatus;
  62.  
  63.     /* open file to write */
  64.     if ((ErrorStatus = xpmWriteFile(filename, &mdata)) != XpmSuccess)
  65.     return (ErrorStatus);
  66.  
  67.     /* figure out a name */
  68.     if (filename) {
  69. #ifdef VMS
  70.     name = filename;
  71. #else
  72.     if (!(name = rindex(filename, '/')))
  73.         name = filename;
  74.     else
  75.         name++;
  76. #endif
  77.     if (dot = index(name, '.')) {
  78.         strcpy(new_name, name);
  79.         /* change '.' to '_' to get a valid C syntax name */
  80.         name = s = new_name;
  81.         while (dot = index(s, '.')) {
  82.         *dot = '_';
  83.         s = dot;
  84.         }
  85.     }
  86.     } else
  87.     name = "image_name";
  88.  
  89.     /* write the XpmData from the XpmImage */
  90.     if (ErrorStatus == XpmSuccess)
  91.     ErrorStatus = xpmWriteData(&mdata, image, name, info);
  92.  
  93.     xpmDataClose(&mdata);
  94.  
  95.     return (ErrorStatus);
  96. }
  97.  
  98. int
  99. xpmWriteData(mdata, image, name, info)
  100.     xpmData *mdata;
  101.     XpmImage *image;
  102.     char *name;
  103.     XpmInfo *info;
  104. {
  105.     /* calculation variables */
  106.     unsigned int cmts, extensions;
  107.     FILE *file;
  108.     int ErrorStatus;
  109.  
  110.     /* store this to speed up */
  111.     file = mdata->stream.file;
  112.  
  113.     cmts = info && (info->valuemask & XpmComments);
  114.     extensions = info && (info->valuemask & XpmExtensions)
  115.     && info->nextensions;
  116.  
  117.     /* print the header line */
  118.     fprintf(file, "/* XPM */\nstatic char * %s[] = {\n", name);
  119.  
  120.     /* print the hints line */
  121.     if (cmts && info->hints_cmt)
  122.     fprintf(file, "/*%s*/\n", info->hints_cmt);
  123.  
  124.     fprintf(file, "\"%d %d %d %d", image->width, image->height,
  125.         image->ncolors, image->cpp);
  126.  
  127.     if (info && (info->valuemask & XpmHotspot))
  128.     fprintf(file, " %d %d", info->x_hotspot, info->y_hotspot);
  129.  
  130.     if (extensions)
  131.     fprintf(file, " XPMEXT");
  132.  
  133.     fprintf(file, "\",\n");
  134.  
  135.     /* print colors */
  136.     if (cmts && info->colors_cmt)
  137.     fprintf(file, "/*%s*/\n", info->colors_cmt);
  138.  
  139.     WriteColors(file, image->colorTable, image->ncolors);
  140.  
  141.     /* print pixels */
  142.     if (cmts && info->pixels_cmt)
  143.     fprintf(file, "/*%s*/\n", info->pixels_cmt);
  144.  
  145.     ErrorStatus = WritePixels(file, image->width, image->height, image->cpp,
  146.                   image->data, image->colorTable);
  147.     if (ErrorStatus != XpmSuccess)
  148.     return (ErrorStatus);
  149.  
  150.     /* print extensions */
  151.     if (extensions)
  152.     WriteExtensions(file, info->extensions, info->nextensions);
  153.  
  154.     /* close the array */
  155.     fprintf(file, "};\n");
  156.  
  157.     return (XpmSuccess);
  158. }
  159.  
  160. static void
  161. WriteColors(file, colors, ncolors)
  162.     FILE *file;
  163.     XpmColor *colors;
  164.     unsigned int ncolors;
  165. {
  166.     unsigned int a, key;
  167.     char *s;
  168.     char **defaults;
  169.  
  170.     for (a = 0; a < ncolors; a++, colors++) {
  171.  
  172.     defaults = (char **) colors;
  173.     fprintf(file, "\"%s", *defaults++);
  174.  
  175.     for (key = 1; key <= NKEYS; key++, defaults++) {
  176.         if (s = *defaults)
  177.         fprintf(file, "\t%s %s", xpmColorKeys[key - 1], s);
  178.     }
  179.     fprintf(file, "\",\n");
  180.     }
  181. }
  182.  
  183.  
  184. static int
  185. WritePixels(file, width, height, cpp, pixels, colors)
  186.     FILE *file;
  187.     unsigned int width;
  188.     unsigned int height;
  189.     unsigned int cpp;
  190.     unsigned int *pixels;
  191.     XpmColor *colors;
  192. {
  193.     char *s, *p, *buf;
  194.     unsigned int x, y, h;
  195.  
  196.     h = height - 1;
  197.     p = buf = (char *) XpmMalloc(width * cpp + 3);
  198.     if (!buf)
  199.     return (XpmNoMemory);
  200.     *buf = '"';
  201.     p++;
  202.     for (y = 0; y < h; y++) {
  203.     s = p;
  204.     for (x = 0; x < width; x++, pixels++) {
  205.         strncpy(s, colors[*pixels].string, cpp);
  206.         s += cpp;
  207.     }
  208.     *s++ = '"';
  209.     *s = '\0';
  210.     fprintf(file, "%s,\n", buf);
  211.     }
  212.     /* duplicate some code to avoid a test in the loop */
  213.     s = p;
  214.     for (x = 0; x < width; x++, pixels++) {
  215.     strncpy(s, colors[*pixels].string, cpp);
  216.     s += cpp;
  217.     }
  218.     *s++ = '"';
  219.     *s = '\0';
  220.     fprintf(file, "%s", buf);
  221.  
  222.     XpmFree(buf);
  223.     return (XpmSuccess);
  224. }
  225.  
  226. static void
  227. WriteExtensions(file, ext, num)
  228.     FILE *file;
  229.     XpmExtension *ext;
  230.     unsigned int num;
  231. {
  232.     unsigned int x, y, n;
  233.     char **line;
  234.  
  235.     for (x = 0; x < num; x++, ext++) {
  236.     fprintf(file, ",\n\"XPMEXT %s\"", ext->name);
  237.     n = ext->nlines;
  238.     for (y = 0, line = ext->lines; y < n; y++, line++)
  239.         fprintf(file, ",\n\"%s\"", *line);
  240.     }
  241.     fprintf(file, ",\n\"XPMENDEXT\"");
  242. }
  243.